package com.itbour.web.action.yjx.trade;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.yjx.trade.YjxSearchTagDto;
import com.itbour.service.yjx.trade.YjxSearchTagService;
import com.opensymphony.xwork2.ActionContext;

public class GetYjxSearchTagAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private YjxSearchTagService yjxSearchTagService;
	private String findTitleName;// 标题名称
	private String yjxSearchTagType;// 根据分类来查询
	HashMap<String, Object> yjxSearchTagResult = new HashMap<String, Object>();
	private YjxSearchTagDto yjxSearchTag;
	private List<YjxSearchTagDto> yjxSearchList;

	public String getSearchTagInfoPg() throws Exception {
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if (StringUtils.isNotBlank(findTitleName)) {
				param.put("type_name", findTitleName);
			}

			if (StringUtils.isNotBlank(yjxSearchTagType)) {
				param.put("type", yjxSearchTagType);
			}
			// 默认只检索自己的文档([超级管理员,模版操作员]除外)
			if (!user.isSuperUser() && (user.getRole() & 32) != 32) {
				param.put("user_id", 3);
			}

			// 翻页查询
			Page page = yjxSearchTagService.pageTagInfoPg(param,
					Integer.parseInt(pageNumber), Integer.parseInt(pageSize));
			HashMap<String, Object> resMap = new HashMap<String, Object>();
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
			yjxSearchTagResult = resMap;
		} else {
			yjxSearchTagResult.clear();
		}
		return SUCCESS;
	}

	// 修改专题显示顺序
	public void updSearchOrder() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("orderid");
		String order = request.getParameter("order");
		if (StringUtils.isNotBlank(id)) {
			Map<String, Object> session = ActionContext.getContext()
					.getSession();
			UserInfoDto user = (UserInfoDto) session.get("user");
			// update
			if (user != null && user.getUser_id() != null) {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("number", Integer.valueOf(order));
				param.put("id", Long.valueOf(id));
				param.put("update_by", user.getUser_id());
				yjxSearchTagService.updSearch(param);
			}
		}
		this.writeJSON("ok");
	}

	// 删除专题信息
	public void delSearch() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		yjxSearchTagService.delSearch(ids);
		this.writeJSON("ok");
	}
	// 添加专题信息
	public void addSearch() throws Exception{
				
		if (yjxSearchTag != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if (user != null) {
	    		yjxSearchTag.setUpdate_by(user.getUser_id().toString());
	    	}
	    	
	    	if(yjxSearchTag.getTag_id()!=null &&yjxSearchTag.getTag_id()>0){
	    		// 更新
	    		this.updSearch();
	    	} else {
	    		// 新建
	    		yjxSearchTagService.addSearch(yjxSearchTag);
	    		this.writeJSON("ok");
	    	}
		} else {
			this.writeJSON("ok");
		}
	
	}
	//更新专题信息
	private void updSearch() throws Exception{
		if (yjxSearchTag != null && yjxSearchTag.getTag_id()>0) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	
	    	Map<String, Object> param = new HashMap<String, Object>();
	    	param.put("id", yjxSearchTag.getTag_id());
	    	if(user != null) {
	    		param.put("update_by", user.getUser_id());
	    	}
	    	if(yjxSearchTag.getType()>0) {
	    		param.put("type", yjxSearchTag.getType());
	    	}

	    	if(yjxSearchTag.getName()!= null) {
	    		param.put("name", yjxSearchTag.getName());//标题
	    	}
	
	       	if(yjxSearchTag.getCreate_time()!= null) {
	    		param.put("start_time", yjxSearchTag.getCreate_time());
	    	}
	       	yjxSearchTagService.updSearch(param);
		}
		this.writeJSON("ok");	
	}
	
	public String getSearchInfoEdit() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (StringUtils.isNotEmpty(id)) {
			param.put("id", id);
		}
		yjxSearchList = yjxSearchTagService.selectSearchInfo(param);
		return SUCCESS;
	}
	
	public YjxSearchTagService getYjxSearchTagService() {
		return yjxSearchTagService;
	}

	public void setYjxSearchTagService(YjxSearchTagService yjxSearchTagService) {
		this.yjxSearchTagService = yjxSearchTagService;
	}

	public String getFindTitleName() {
		return findTitleName;
	}

	public void setFindTitleName(String findTitleName) {
		this.findTitleName = findTitleName;
	}

	public String getYjxSearchTagType() {
		return yjxSearchTagType;
	}

	public void setYjxSearchTagType(String yjxSearchTagType) {
		this.yjxSearchTagType = yjxSearchTagType;
	}

	public HashMap<String, Object> getYjxSearchTagResult() {
		return yjxSearchTagResult;
	}

	public void setYjxSearchTagResult(HashMap<String, Object> yjxSearchTagResult) {
		this.yjxSearchTagResult = yjxSearchTagResult;
	}

	public YjxSearchTagDto getYjxSearchTag() {
		return yjxSearchTag;
	}

	public void setYjxSearchTag(YjxSearchTagDto yjxSearchTag) {
		this.yjxSearchTag = yjxSearchTag;
	}

	public List<YjxSearchTagDto> getYjxSearchList() {
		return yjxSearchList;
	}

	public void setYjxSearchList(List<YjxSearchTagDto> yjxSearchList) {
		this.yjxSearchList = yjxSearchList;
	}

}
